package day01;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.MessageProperties;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * <>
 * <详细描述>
 *
 * @Author WANG.SONG.
 * @Date 2019/3/18 0018 22:36
 * @see
 */
public class RabbitProducer {
    private static final String EXCHANGE_NAME =  "exchangedemo";
    private static final String ROUTING_KEY =  "routingkeydemo";
    private static final String QUEUE_NAME =  "queuedemo";
    private static final String IP_ADDRESS =  "192.168.1.112";
    //RabbitMQ服务端默认端口号为5672
    private static final int PORT  =  5672;

    public static void main(String[] args) throws IOException, TimeoutException {
        ConnectionFactory factory =new ConnectionFactory();
        factory.setHost(IP_ADDRESS);
        factory.setPort(PORT);
        factory.setUsername("guest");
        factory.setPassword("guest");
        //创建连接
        Connection connection =factory.newConnection();
        //创建通信
        Channel channel = connection.createChannel();
        //创建一个type="direct"、持久化的、非自动删除的交换器
        channel.exchangeDeclare(EXCHANGE_NAME,"direct",true,false,null);
        //创建一个持久化、非排他的、非自动删除的队列
        channel.queueDeclare(QUEUE_NAME,  true,  false,  false,  null);
        //将交换器与队列通过路由键绑定
        channel.queueBind(QUEUE_NAME,  EXCHANGE_NAME,  ROUTING_KEY);

        String message = "hello world!";

        channel.basicPublish(EXCHANGE_NAME,ROUTING_KEY, MessageProperties.PERSISTENT_TEXT_PLAIN,message.getBytes());
        channel.close();
        connection.close();
    }
}
